﻿using ImageManager.MyType;

namespace ImageManager.MyType.FieldType
{
    [Serializable, MyType]
    public class MyDateTime : IDynamicSet, IMyPropColumnType
    {
        const string FORMAT = "yyyy-MM-dd HH:mm:ss";
        public static MyDateTime Now()
        {
            return new MyDateTime(DateTime.Now);
        }
        public MyDateTime()
        {
        }
        public MyDateTime(object o)
        {
            InitWithVal(o);
        }
        MyDateTime(DateTime dt)
        {
            dtVal = dt;
        }
        MyDateTime(string dtStr)
        {
            DateTime.TryParse(dtStr, out dtVal);
        }
        private DateTime dtVal = DateTime.MinValue;
        public override string ToString()
        {
            return dtVal.ToString(FORMAT);
        }

        public IDynamicSet InitWithVal(object val)
        {
            DateTime.TryParse(val?.ToString(), out dtVal);
            return this;
        }

        public static implicit operator MyDateTime(DateTime val)
        {
            return new MyDateTime(val);
        }
        public static implicit operator MyDateTime(string val)
        {
            return new MyDateTime(val);
        }
        public override int GetHashCode()
        {
            return this.dtVal.GetHashCode();
        }
        public override bool Equals(object? obj)
        {
            if (obj == null) return false;
            MyDateTime o = (MyDateTime)obj;
            return o.dtVal.Equals(this.dtVal);
        }

        public object CreateWithVal(object val)
        {
            if (val == null) return new MyDateTime();
            return new MyDateTime(val.ToString()!);
        }

        public object GetVal()
        {
            return this.ToString();
        }
    }
}
